/**
 * file
 */
package com.tdkj.action.front.member.spaceManagement;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.enterprisezone.BeanEnterprise;
import com.tdkj.model.enterprisezone.BeanEnterpriseZone;
import com.tdkj.model.enterprisezone.BeanEnterpriseZoneFriendsLink;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.enterprisezone.IBeanEnterpriseNewsService;
import com.tdkj.service.enterprisezone.IBeanEnterpriseZoneFriendsLinkService;
import com.tdkj.service.enterprisezone.impl.BeanEnterpriseZoneFriendsLinkServiceImpl;
import com.tdkj.service.user.IBeanUserService;
import com.tdkj.util.PublicConfing;

/**

 * FileName:     SpaceLinkSettingsAction.java

 * @Description: TODO友情链接设置

 * All rights Reserved, Designed By ZTE-ITS

 * Copyright:    Copyright(C) 2010-2011

 * Company       天成科技有限公司

 * @author:    李兴建

 * @version    V1.0 

 * Createdate:         2012-5-27 上午11:51:30

 *

 * Modification  History:

 * Date         Author        Version        Discription

 * -----------------------------------------------------------------------------------

 * 2012-5-27       李兴建          1.0             1.0

 * Why & What is modified: <修改原因描述>

 */
public class SpaceLinkSettingsAction extends BaseAction {
	
	
	private String friendName;
	//友情链接列表
	private List<BeanEnterpriseZoneFriendsLink> friendsLinkList;
	private IBeanEnterpriseZoneFriendsLinkService enterpriseZoneFriendsLinkService;
	private IBeanUserService userService;
	
	@Override
	public String execute(){
		BeanUser user =  (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		BeanEnterpriseZone zone = user.getEnterpriseZone();
		friendsLinkList = enterpriseZoneFriendsLinkService.findByProperty("enterpriseZone", zone, BeanEnterpriseZoneFriendsLink.class);
		for(int i = 0 ; i < friendsLinkList.size() ; i ++){
			String name = friendsLinkList.get(i).getEnterpriseZone().getName();
		}
		return "success";
	}
	
	public String save(){
		Boolean isExistUser = enterpriseZoneFriendsLinkService.isValidMemberName(friendName);
		if(isExistUser){
			BeanUser user =  (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
			BeanEnterpriseZone enterpriseZone = user.getEnterpriseZone();
			BeanEnterpriseZoneFriendsLink friendsLink = new BeanEnterpriseZoneFriendsLink();
			friendsLink.setEnterpriseZone(enterpriseZone);
			friendsLink.setFriend(userService.findByProperty("username" , friendName , BeanUser.class).get(0));
			enterpriseZoneFriendsLinkService.save(friendsLink);
			friendName = "";//清除页面表单中数据
		}
		return execute();
	}
	

	public String delete(){
		Long linkid = Long.valueOf( ServletActionContext.getRequest().getParameter("linkid"));
		BeanEnterpriseZoneFriendsLink friendLink = enterpriseZoneFriendsLinkService.findById(linkid, BeanEnterpriseZoneFriendsLink.class);
		enterpriseZoneFriendsLinkService.delete(friendLink);
		return execute();
	}


	public String getFriendName() {
		return friendName;
	}

	public void setFriendName(String friendName) {
		this.friendName = friendName;
	}

	public List<BeanEnterpriseZoneFriendsLink> getFriendsLinkList() {
		return friendsLinkList;
	}

	public void setFriendsLinkList(
			List<BeanEnterpriseZoneFriendsLink> friendsLinkList) {
		this.friendsLinkList = friendsLinkList;
	}

	@Resource
	public void setEnterpriseZoneFriendsLinkService(
			IBeanEnterpriseZoneFriendsLinkService enterpriseZoneFriendsLinkService) {
		this.enterpriseZoneFriendsLinkService = enterpriseZoneFriendsLinkService;
	}
	
	@Resource
	public void setUserService(IBeanUserService userService) {
		this.userService = userService;
	}

}
